<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="elementUI 源码分析​    最近想学一下如何写一个组件，所以学习一下elementUI的源码，大概有一个基本的了解。 入口 package.json​    主要的几个地方 12main，定义一个入口文件，默认值是模块根目录的index.jsstyle，打包工具通过style知道了样式文件的打包位置    src&#x2F;index.js​    我们看一下 src的index ​    其实大">
<meta property="og:type" content="article">
<meta property="og:title" content="elementUI源码分析(一)">
<meta property="og:url" content="http://tallgy.gitee.io/2022/01/29/elementUI%E6%BA%90%E7%A0%81/elementUI%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E4%B8%80)/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="elementUI 源码分析​    最近想学一下如何写一个组件，所以学习一下elementUI的源码，大概有一个基本的了解。 入口 package.json​    主要的几个地方 12main，定义一个入口文件，默认值是模块根目录的index.jsstyle，打包工具通过style知道了样式文件的打包位置    src&#x2F;index.js​    我们看一下 src的index ​    其实大">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-29T09:18:30.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="elementUI">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2022/01/29/elementUI%E6%BA%90%E7%A0%81/elementUI%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E4%B8%80)/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/01/29/elementUI%E6%BA%90%E7%A0%81/elementUI%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E4%B8%80)/","path":"2022/01/29/elementUI源码/elementUI源码分析(一)/","title":"elementUI源码分析(一)"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>elementUI源码分析(一) | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#elementUI-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90"><span class="nav-number">1.</span> <span class="nav-text">elementUI 源码分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%A5%E5%8F%A3-package-json"><span class="nav-number">1.1.</span> <span class="nav-text">入口 package.json</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#src-index-js"><span class="nav-number">1.2.</span> <span class="nav-text">src&#x2F;index.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F"><span class="nav-number">1.3.</span> <span class="nav-text">组件的文件格式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#index-js"><span class="nav-number">1.3.1.</span> <span class="nav-text">index.js</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#index-scss"><span class="nav-number">1.4.</span> <span class="nav-text">index.scss</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#style-sass"><span class="nav-number">1.5.</span> <span class="nav-text">style sass</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AF%B9%E4%BA%8E-placeholder"><span class="nav-number">1.5.1.</span> <span class="nav-text">对于 placeholder</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#disabled"><span class="nav-number">1.5.2.</span> <span class="nav-text">disabled</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/01/29/elementUI%E6%BA%90%E7%A0%81/elementUI%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E4%B8%80)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          elementUI源码分析(一)
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-29 17:18:30" itemprop="dateCreated datePublished" datetime="2022-01-29T17:18:30+08:00">2022-01-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/elementUI/" itemprop="url" rel="index"><span itemprop="name">elementUI</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="elementUI-源码分析"><a href="#elementUI-源码分析" class="headerlink" title="elementUI 源码分析"></a>elementUI 源码分析</h1><p>​    最近想学一下如何写一个组件，所以学习一下elementUI的源码，大概有一个基本的了解。</p>
<h2 id="入口-package-json"><a href="#入口-package-json" class="headerlink" title="入口 package.json"></a>入口 package.json</h2><p>​    主要的几个地方</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">main，定义一个入口文件，默认值是模块根目录的index.js</span><br><span class="line">style，打包工具通过style知道了样式文件的打包位置</span><br></pre></td></tr></table></figure>



<h2 id="src-index-js"><a href="#src-index-js" class="headerlink" title="src/index.js"></a>src/index.js</h2><p>​    我们看一下 src的index</p>
<p>​    其实大致的意思就是，先进行组件的引入，然后将引入的组件合并为一个数组，然后创建一个 install 方法。里面的核心就是 Vue.component 方法。最后在export 导出组件和install方法。</p>
<p>​    install方法为什么需要呢，因为在调用 Vue.use 时，如果插件是一个对象，那么就会查找里面的install方法。如果插件是一个函数，那么就会作为install方法进行调用。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">import xx from &#x27;xxx&#x27;</span><br><span class="line"></span><br><span class="line">const c = [</span><br><span class="line">	xx</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">const install = function(Vue, opts = &#123;&#125;) &#123;</span><br><span class="line">	c.forEach(v =&gt; &#123;</span><br><span class="line">		Vue.component(v.name, v)</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default &#123;</span><br><span class="line">	install,</span><br><span class="line">	</span><br><span class="line">	xx</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里，我个人觉得使用 对象来代替数组进行存储。可以将后续的export default 使用解构赋值来进行代替。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">const c = &#123;</span><br><span class="line">	xx</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const install = function(Vue, opts = &#123;&#125;) &#123;</span><br><span class="line">	for (const key in c) &#123;</span><br><span class="line">		Vue.component(key, c[key])</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default &#123;</span><br><span class="line">	install,</span><br><span class="line">	</span><br><span class="line">	...c</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="组件的文件格式"><a href="#组件的文件格式" class="headerlink" title="组件的文件格式"></a>组件的文件格式</h2><p>创建一个目录，然后创建一个index.js文件作为一个入口，创建一个src文件夹作为组件的存放。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">xx</span><br><span class="line">	src</span><br><span class="line">		xx.vue</span><br><span class="line">	index.js</span><br></pre></td></tr></table></figure>



<h3 id="index-js"><a href="#index-js" class="headerlink" title="index.js"></a>index.js</h3><p>其中，index.js 才是比较重要的一个部分</p>
<p>因为在使用 from 进行引入时，如果是一个目录，那么默认就会去查找index.js文件，当然如果配置了package.json的入口会不一样。</p>
<p>​    index.js的内容，简单来说就是创建了一个install方法。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import xx from &#x27;../src/xx&#x27;</span><br><span class="line"></span><br><span class="line">xx.install = function(Vue) &#123;</span><br><span class="line">	Vue.component(xx.name, xx)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export defalt xx</span><br></pre></td></tr></table></figure>



<h2 id="index-scss"><a href="#index-scss" class="headerlink" title="index.scss"></a>index.scss</h2><p>​        在 package/theme-chalk/src 里面。</p>
<p>​        common，里面创建一个 var.scss 里面存储了一些公用的变量。其他地方需要时要引入 var.scss</p>
<p>​        以及在一个index.scss 里面进行一个引入。</p>
<p>​        然后再看看 element ui的官网。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import &#x27;element-ui/lib/theme-chalk/index.css&#x27;;</span><br><span class="line">这个里面就是存储了全局的css，然后进行一个引入。</span><br></pre></td></tr></table></figure>

<p>​        并且将icon.scss 进行了引入，然后通过全局引入index来引入的icon。</p>
<p>​        原因：</p>
<ul>
<li><p>因为 iconfont 的引入是需要使用url的引入</p>
<ul>
<li>```<br>src: url(‘./fonts/iconfont.ttf?t=1643365814886’) format(‘truetype’);<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 同时对于引用的一个问题。那就是如果我在input目录下的index.scss 进行引入。那么对应的需要将引入的url路径进行改变，例子：url(&#x27;../fonts/iconfont.ttf&#x27;)</span><br><span class="line"></span><br><span class="line">  * 因为执行文件在这个目录，所以最终scss编译为css的时候会将路径引入在这里。所以会出现问题，所以Elem UI 的方法是将其在外面创建一个index.scss，然后只需要引入这个index.scss，即可。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
fonts<br>iconfont.ttf<br>input<br>index.scss<br>icon.scss<br>index.scss<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## fonts-icon</span><br><span class="line"></span><br></pre></td></tr></table></figure>
package/theme-chalk/src/icon.scss<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 首先，这个fonts-icon 是使用的 font class 这个方法，具体的几个方法。我们可以通过去 阿里巴巴矢量图标库下载时，使用下载代码，查看demo的html来进行查看区分。</span><br><span class="line"></span><br><span class="line">* 使用 @font-face 进行初始化。这个参数需要进行了解。这个大概就是说，指定一个font-family的名称，以及指定一个url代表了可以去哪进行获取。</span><br><span class="line">  * 使用 #&#123;$xx&#125; 让 路径成为一个变量。</span><br><span class="line">* 然后使用类名选择器进行查找类名，将对应类名的进行初始化项目。</span><br><span class="line">* 目录构架，创建一个fonts的文件夹，用来存放 tff 和 字体的 js wtff 等文件，然后在外面创建一个icon的scss文件，用来进行使用。</span><br><span class="line">* 然后对要调用的使用类进行调用，然后类将同一进行一个css选择器进行初始化和选择。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">​		这里我们看一下代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>
[class^=”el-icon-“], [class*=” el-icon-“] {    }<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">​		这是element 写的选择器的选择，这两个选择器分别代表 选择从 el-icon- 开始 或者 包含了 &#x27; el-icon-&#x27;，这里，你可能会有思考，为什么需要分开，如果我直接使用 class*=&quot;el-icon-&quot; 这个不就是包含了吗。开始我也在思考，然后我发现了。因为 *= 是包含。那么如果用户自己定义的类名是 xxel-icon- 那么也会被我们这个包含进去，所以 这个就是需要进行分开，代表了是一个独立的class。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## input</span><br><span class="line"></span><br></pre></td></tr></table></figure>
package/inpu<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">​		Elem UI 使用 input的方法是。首先，通过type进行类型的决定，并且支持 textarea 类型。</span><br><span class="line"></span><br><span class="line">​		输入框包含了 前/后 置元素 和 内容。 内容就是显示在输入框里面内部的icon等。 元素就是显示在输入框前面的。和输入框紧邻的一个样式。 </span><br><span class="line"></span><br><span class="line">​		同时，禁用了组件的继承，然后将属性全部赋值于input 输入框里面，方便用于进行透明化的处理。这个常用于封装组件，并且组件的核心不处于根组件的情况。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
v-bind=”$attrs”</li>
</ul>
</li>
</ul>
<p>inheritAttrs: false</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">​		同时 Elem UI 的密码框是通过使用 show-password 进行的操作。</span><br><span class="line"></span><br><span class="line">### icon 部分</span><br><span class="line"></span><br><span class="line">​		并且使用了 前置内容 和 后置内容 通过使用 prefixIcon 和 suffixIcon 来进行设置。并且在里面同时也设置了 插槽。可以使用插槽的形式来配置前置和后置 内容。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><slot name="prefix"></slot><br><i class="el-input__icon" v-if="prefixIcon" :class="prefixIcon"><br></i></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">​		使用了 v-if 对于没有使用icon的进行了 隐藏。</span><br><span class="line"></span><br><span class="line">​		如果使用了 prefixIcon 进行了图标，或者 $slots.prefix 插槽。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>span<br>    v-if=”prefixIcon || $slots.prefix”</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### input事件部分</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>@compositionstart=”handleCompositionStart”<br>@compositionupdate=”handleCompositionUpdate”<br>@compositionend=”handleCompositionEnd”<br>@input=”handleInput”<br>@focus=”handleFocus”<br>@blur=”handleBlur”<br>@change=”handleChange”</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">**这里的问题点：** </span><br><span class="line"></span><br><span class="line">* 我们为什么不使用 $listeners 进行封装。</span><br><span class="line"></span><br><span class="line">* 我们可以知道，默认情况下，进行组件方法的监听会加载入根组件</span><br><span class="line"></span><br><span class="line">* 但是，因为组件内部的根组件不是input，所以对于监听一个原生的方法。我们使用.native。</span><br><span class="line"></span><br><span class="line">* 并且直接监听给根组件并不会被当作一个原生的监听方法。对于根组件的监听，我们就可以通过 $listeners 进行监听。</span><br><span class="line"></span><br><span class="line">* 那么我好奇，如果我 使用 v-model，然后，组件内部使用 v-bind=&#x27;$attrs&#x27; v-on=&quot;$listeners&quot; 那么是不是就应该也有对应的效果啊。</span><br><span class="line"></span><br><span class="line">* 当然事实是不行，因为数据返回是一个 input event 事件。所以我看到了vue 官网是使用的 this.$emit(&#x27;input&#x27;, event.target.value) 然后就可以了。</span><br><span class="line"></span><br><span class="line">* 虽然这里，可以使用 v-model 了，但是，有另一个问题，那就这样写，自身的input事件会不一样。</span><br><span class="line"></span><br><span class="line">  * 首先 使用 @input=&quot;xxFn&quot; 此时的第一个参数不是event事件了，而是子组件传递过来的参数</span><br><span class="line">  * 其次，如果我们使用 focus.native 那么就会失败。但是因为input是一个可冒泡事件，所以input使用没有问题。</span><br><span class="line"></span><br><span class="line">* 同时注意，此时的$event 是子组件传递的数据了。</span><br><span class="line"></span><br><span class="line">  * ```</span><br><span class="line">    inputListeners: function () &#123;</span><br><span class="line">      var vm = this</span><br><span class="line">      // `Object.assign` 将所有的对象合并为一个新对象</span><br><span class="line">      return Object.assign(&#123;&#125;,</span><br><span class="line">        // 我们从父级添加所有的监听器</span><br><span class="line">        this.$listeners,</span><br><span class="line">        // 然后我们添加自定义监听器，</span><br><span class="line">        // 或覆写一些监听器的行为</span><br><span class="line">        &#123;</span><br><span class="line">          // 这里确保组件配合 `v-model` 的工作</span><br><span class="line">          input: function (event) &#123;</span><br><span class="line">            vm.$emit(&#x27;input&#x27;, event.target.value)</span><br><span class="line">          &#125;,</span><br><span class="line">        &#125;</span><br><span class="line">      )</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    v-on=&quot;inputListeners&quot;</span><br><span class="line">    </span><br><span class="line">    父组件的调用</span><br><span class="line">    方法</span><br><span class="line">    @input=&quot;inputFn(&#x27;$event&#x27;, $event)&quot;</span><br><span class="line">    inputFn(event, e) &#123;</span><br><span class="line">      console.log(&#x27;event&#x27;, event, e) // $event, value</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>

<p><strong>Elem UI input 的监听事件之 compositionstart</strong>： </p>
<p>通过使用 isComposing 来判断是否处于中文输入情况。对于中文的输入不会提交，不会进行验证和发送。</p>
<p>其中我们发现在update的时候，调用了一个 isKorean 这里的方法就是里面有一个正则判断来判断你的输入是不是韩文的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const text = event.target.value;</span><br><span class="line">const lastCharacter = text[text.length - 1] || &#x27;&#x27;;</span><br><span class="line">this.isComposing = !isKorean(lastCharacter);</span><br><span class="line"></span><br><span class="line">判断是不是韩文。</span><br><span class="line">export function isKorean(text) &#123;</span><br><span class="line">  const reg = /([(\uAC00-\uD7AF)|(\u3130-\u318F)])+/gi;</span><br><span class="line">  return reg.test(text);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        同时我发现，Elem UI对于 compositionstart 事件没有进行 emit 提交。所以我这里给我的UI做了一个提交。不知道是不是应该有个什么问题。</p>
<p>​        同时，注意Elem UI input并没有对date进行封装样式，只是另写了一个组件给的date样式，内部input实则还是String。</p>
<p>​        输入框的 清除框通过v-if进行的判断显示隐藏。通过focus事件和mouseenter和mouseleave来解决 focus 和 hover 的值。</p>
<p>输入框的 placeholder 样式使用的 scss的 mixin 和 content 进行的混入。</p>
<h2 id="style-sass"><a href="#style-sass" class="headerlink" title="style sass"></a>style sass</h2><p>​        样式的编写。</p>
<h3 id="对于-placeholder"><a href="#对于-placeholder" class="headerlink" title="对于 placeholder"></a>对于 placeholder</h3><p>​        使用 混入 和 content 进行了 一次 封装，后续只需要混入 这个placeholder 方法就可以进行处理。当然，我发现对于input貌似不需要使用这个混入，直接使用 ::placeholder 这个方法就可以。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@mixin placeholder &#123;</span><br><span class="line">  &amp;::-webkit-input-placeholder &#123;</span><br><span class="line">    @content</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &amp;::-moz-placeholder &#123;</span><br><span class="line">    @content</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &amp;:-ms-input-placeholder &#123;</span><br><span class="line">    @content</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="disabled"><a href="#disabled" class="headerlink" title="disabled"></a>disabled</h3><p>​        同时使用混入进行disabled的方法的处理。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/elementUI/" rel="tag"># elementUI</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/01/22/%E9%9A%8F%E7%AC%94/npm-package%E4%B8%8A%E4%BC%A0/" rel="prev" title="npm-package上传">
                  <i class="fa fa-chevron-left"></i> npm-package上传
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/02/02/webpack/webpack%E5%AD%A6%E4%B9%A0%E4%B8%80/" rel="next" title="webpack学习一">
                  webpack学习一 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
